using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for BrowseableList.
	/// </summary>
	public abstract class BrowseableList : IBrowseableList
	{
		protected abstract ApplicationType ApplicationType {get;}
		
		#region IBrowseableList Members

		public virtual HyperLink AddItemUrl { get{ return null; } }

		public virtual System.Collections.ArrayList Items { get{ return null; } }

		public abstract string Name {get;}

		public abstract System.Collections.ArrayList Containers {get;}

		public abstract object GetNode(string listKey);

		public abstract StringCollection GetPathToNode(string listKey);

		public virtual ArrayList GetBrowseableListOptions()
		{
			return new ArrayList();
		}

		public virtual bool Enabled
		{
			get
			{
				return ApplicationSet.Applications[this.ApplicationType].Enabled;
			}
		}

		#endregion
		
		#region Helpers
		
		public static bool UserCanView(Section s, User user)
		{

			if (!Permissions.ValidatePermissions(s, Permission.View, user))
				return false;

			if (!s.IsActive)
				return false;
            
			//not used currently
			//if (!s.IsVisible)
			//    return false;

			if (s.IsPrivate)
				return false;

			return true;
		}

		public static string MakeListKey(int groupID, int sectionID, int categoryID, int postID, string parentCategoryIDs)
		{
			if (postID > 0)
				return "P" + postID.ToString() + ":P" + parentCategoryIDs;

			if (categoryID > 0)
				return "C" + categoryID.ToString() + ":" + "S" + sectionID.ToString() + ":P" + parentCategoryIDs;

			if (sectionID > 0)
				return "S" + sectionID.ToString();

			if (groupID > 0)
				return "G" + groupID.ToString();

			return "";
		}
		
		#endregion
	}

	public abstract class BrowseableListContainer : IBrowseableListContainer
	{
		private Group _group;
		private Section _section;
		private PostCategory _category;
		private string _parentCategoryIDs;

		public BrowseableListContainer(Group group, Section section, PostCategory category, string parentCategoryIDs)
		{
			_group = group;
			_section = section;
			_category = category;
			_parentCategoryIDs = parentCategoryIDs;
		}

		#region IBrowseableListContainer Members

		public virtual HyperLink AddItemUrl { get{ return null; } }

       
		public virtual bool UserCanPost(User user)
		{
			if (_section != null)
				return Permissions.ValidatePermissions(_section, Permission.Post, user);
			else
				return false;
		}

		public virtual string ListKey
		{
			get
			{
				return BrowseableList.MakeListKey(_group != null ? _group.GroupID : -1, _section != null ? _section.SectionID : -1, _category != null ? _category.CategoryID : -1, -1,  _parentCategoryIDs);
			}
		}

		public virtual System.Collections.ArrayList Items { get{ return null; } }


		public virtual string Name
		{
			get
			{
				if (_category != null)
					return _category.Name;

				if (_section != null)
					return _section.Name;

				if (_group != null)
					return _group.Name;

				return "";
			}
		}

		public virtual System.Collections.ArrayList Containers{ get{ return null; } }

		#endregion


	}

	public abstract class BrowseableListItem : IBrowseableListItem
	{
		private Post _post;
		private string _parentCategoryIDs;

		public virtual Post Post
		{
			get{return _post;}
			set{_post = value;}
		}

		public BrowseableListItem(Post post, string parentCategoryIDs)
		{
			Post = post;
			_parentCategoryIDs = parentCategoryIDs;
		}

		#region IBrowseableListItem Members

		public string ListKey
		{
			get
			{
				return BrowseableList.MakeListKey(-1, Post.SectionID, -1, Post.PostID, _parentCategoryIDs);
			}
		}

		public string Name
		{
			get
			{
				return Post.Subject;
			}
		}

		public virtual string GetRenderHTML()
		{
			return GetRenderHTML(new NameValueCollection());
		}

		public abstract string GetRenderHTML(NameValueCollection optionValues);

		public abstract string PreviewHTML {get;}

		#endregion

	}
}
